﻿using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Editor.XNA
{
    public class RenderWindow : Editor.GraphicsDeviceControl
    {

        Stopwatch timer;
        public ContentManager content;
        public SpriteBatch theBatch;
        Texture2D elite;

     
        public Vector2 elitePos;
        public Vector2 eliteVel;
        public float Speed;
        // Vertex positions and colors used to display a spinning triangle.


        public Color BackgroundColor;
        /// <summary>
        /// Initializes the control.
        /// </summary>
        /// 
        protected override void Initialize()
        {


            timer = Stopwatch.StartNew();
            BackgroundColor = Color.Black;
            content = new ContentManager(Services, "Content");
            theBatch = new SpriteBatch(GraphicsDevice);
            elite = content.Load<Texture2D>("Smiley");
            elitePos.X = this.Width / 2;
            elitePos.Y = this.Height / 2;
            eliteVel = new Vector2(0.5f, 0.5f);
            Speed = 3;
            // Hook the idle event to constantly redraw our animation.
            Application.Idle += delegate { Invalidate(); };
        }

        ///<summary>
        ///Updates any objects that need updating
        ///</summary>
        ///
        void Update(float theElapsedTime)
        {
            elitePos += Speed * eliteVel;
            if (elitePos.X > (this.Width - 125) || elitePos.X < 0)
                eliteVel.X *= -1;
            if (elitePos.Y > (this.Height - 125) || elitePos.Y < 0)
                eliteVel.Y *= -1;        
            
        }
        
        /// <summary>
        /// Draws the control.
        /// </summary>
        protected override void Draw()
        {
            // Spin the triangle according to how much time has passed.
            float time = (float)timer.Elapsed.TotalSeconds;
            
            Update(time);
            GraphicsDevice.Clear(BackgroundColor);
            
            theBatch.Begin();
            theBatch.Draw(elite, elitePos, Color.White);
            theBatch.End();

            // Set renderstates.
            // GraphicsDevice.RenderState.CullMode = CullMode.None;    

        }

        /// <summary>
        /// Disposes the control, unloading the ContentManager.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                content.Unload();
            }

            base.Dispose(disposing);
        }


    }
}
